package com.wwb.w0421;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    Spinner spinner;
    EditText ed_num1,ed_num2;
    Button button;
    TextView tv_show;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        String[] ops={"+","-","*","/"};
        spinner.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,
                ops
        ));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int pos=spinner.getSelectedItemPosition();
                int num1=Integer.parseInt(ed_num1.getText().toString());
                int num2=Integer.parseInt(ed_num2.getText().toString());
                int result=0;
                switch (pos){
                    case 0:
                        result=num1+num2;
                        break;
                    case 1:
                        result=num1-num2;
                        break;
                    case 2:
                        result=num1*num2;
                        break;
                    case 3:
                        if (num2==0){
                            Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show();
                            //tv_show.setText("除数不能为0");
                            return ;
                        }else{
                            result=num1/num2;
                        }
                        break;
                }
                tv_show.setText("您的计算结果为"+result);
            }
        });




    }


    public void init() {
        spinner=findViewById(R.id.spinner);
        ed_num1=findViewById(R.id.ednum1);
        ed_num2=findViewById(R.id.ednum2);
        button=findViewById(R.id.button);
        tv_show=findViewById(R.id.tv_show);
    }

//    public void showdialog(View view){
//        Context context;
//        AlertDialog.Builder builder =new AlertDialog.Builder(this);
//        builder.setIcon(R.drawable.ic_launcher_background)
//                .setTitle("这是由武文博制作的计算器，哈哈哈哈哈哈哈嗝")
//                .setMessage("你要退出吗？")
//                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        System.out.println("ta点击了OK");
//                    }
//                }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//
//            }
//        }).create().show();
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
//        SubMenu sb=menu.addSubMenu("关于");
//        sb.add("哈哈哈");
//        menu.addSubMenu("退出");
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
//    你要退出吗？
//    这是由武文博制作的计算器，哈哈哈哈哈哈哈嗝
//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item){
//        return super.onOptionsItemSelected(item);
//    }
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.about:
            AlertDialog.Builder builder =new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.wwb)
                    .setTitle("关于")
                    .setMessage("这是由武文博制作的计算器")
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            System.out.println("ta点击了OK");
                        }
                    }).setNegativeButton("", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            }).create().show();
            break;
        case R.id.back:
            this.finish();
            break;
    }
    return super.onOptionsItemSelected(item);

}


}